/**
 * Main.java [V 1.0.0]
 * classes : com.xiaocq.iwordkiller.Main
 * xiaocq Create at 2012-4-10 下午6:05:10
 */
package com.xiaocq.iwordkiller;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.xiaocq.iwordkiller.db.PrepareCursorHelper;

/**
 * com.xiaocq.iwordkiller.Main
 * @author Mystery
 * Create at 2012-4-10 下午6:05:10
 */
public class Main extends Activity{
	private static final String TAG = "Main";
	
	private static final String IS_FIRST_LAUNCH = "isFirstLaunch";
	private static final int IMPORT_DIALOG = 1;
	private static final int EXIT_DIALOG = 2;
	
	private boolean mFirstLaunch = true;
	
	private Button mTest,mWrongRecords,mStatistic,mSetting;
	private ImportTask mTask;
	private SharedPreferences pref;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		pref = getSharedPreferences(Param.PARAM_NAME, MODE_PRIVATE);
		mFirstLaunch = pref.getBoolean(IS_FIRST_LAUNCH, true);
		if(mFirstLaunch){
			showDialog(IMPORT_DIALOG);
		}
		setContentView(R.layout.main);
		initComponents();
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		AlertDialog.Builder builder;
		switch(id){
		case IMPORT_DIALOG:
			builder = new AlertDialog.Builder(this);
			builder.setMessage(R.string.tip_first_using);
			builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					mTask = new ImportTask();
					mTask.execute();
				}
			});
			builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
				}
			});
			return builder.create();
		case EXIT_DIALOG:
			builder = new AlertDialog.Builder(this);
			builder.setMessage(R.string.tip_exit);
			builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					finish();
					System.exit(0);
				}
			});
			builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
				}
			});
			return builder.create();
		default:
			return super.onCreateDialog(id);
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			showDialog(EXIT_DIALOG);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	private void initComponents(){
		mTest = (Button) findViewById(R.id.main_gotoTest);
		mWrongRecords = (Button) findViewById(R.id.main_gotoWrongSet);
		mStatistic = (Button) findViewById(R.id.main_gotoStatistic);
		mSetting = (Button) findViewById(R.id.main_gotoSetting);
		
		mTest.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if(mFirstLaunch){
					showDialog(IMPORT_DIALOG);
					return;
				}
				Intent intent = new Intent();
				intent.setClass(Main.this, ParamTest.class);
				startActivity(intent);
			}
		});
		
		mStatistic.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(Main.this, Statistic.class);
				startActivity(intent);
			}
		});
	}
	
	private class ImportTask extends AsyncTask<Void, Void, Void>{
		private ProgressDialog progressDialog;
		private PrepareCursorHelper helper;

		private long startTimeInMillis,finishTimeInMillis;
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			
			progressDialog = new ProgressDialog(Main.this);
			progressDialog.setMessage(getText(R.string.tip_loading));
			progressDialog.setCancelable(false);
			progressDialog.show();
			startTimeInMillis = System.currentTimeMillis();
			helper = new PrepareCursorHelper(Main.this);
			helper.open();
		}
		@Override
		protected Void doInBackground(Void... params) {
			helper.initDatabase();
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			
			finishTimeInMillis  = System.currentTimeMillis();
			
			helper.close();
			
			mFirstLaunch = false;
			SharedPreferences.Editor editor = pref.edit();
			editor.putBoolean(IS_FIRST_LAUNCH, mFirstLaunch);
			editor.commit();
			
			progressDialog.dismiss();
			
			Toast.makeText(Main.this, R.string.tip_import_success, Toast.LENGTH_SHORT).show();
			
			Log.d(TAG, "Cost: " + (finishTimeInMillis - startTimeInMillis) + " ms");
		}
	}
}
